package com.xhui.vimarket.zkplus.zul.attr.evt.withsub;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Toolbarbutton;
com.xhui.vimarket.zkplusui.vimarket.zkplus.util.com.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpluscom.xhui.vimarket.zkplusxt;
import com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusan.AttrInst;
import com.xhui.vicom.xhui.vimarket.zkplusul.attr.bean.EvtData;
import com.xhui.vimcom.xhui.vimarket.zkplusl.attr.bean.impl.AttrInstImpl;
import ccom.xhui.vimarket.zkplust.zkplus.zul.attr.evt.AttrBaseComposer;
com.xhui.vimarket.zkplus.vimarket.zkplus.zul.attr.mod.AttrCompCcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zul.attrcom.xhui.vimarket.zkplusctory;
import com.xhui.vimarket.zkplus.zcom.xhui.vimarket.zkplusrCompUtil;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrCheckboxComp;

/**
 * .
 * 
 * @版权：福富软件 版权所有 (c) 2011
 * @author wuyx
 * @version Revision 1.0.0
 * @see:
 * @创建日期：2011-6-3
 * @功能说明：
 * 
 */
public class AttrCheckboxComposer extends AttrBaseComposer {
    
    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 4579587521568091904L;
    /**
     * 日志. 
     */
    private final Logger      logger           = Logger.getLogger(AttrCheckboxComposer.class);
    
    @Override
    public ComponentInfo doBeforeCompose(final Page page, final Component comp,
        final ComponentInfo info) {
        return info;
    }
    
    @Override
    public void doAfterCompose(final Component comp) throws Exception {
        super.doAfterCompose(comp);
        // 初始化
        this.init(comp);
    }
    
    /**
     * init.
     * @param comp Component
     */
    public void init(final Component comp) {
        final Toolbarbutton tbbtnShow = new Toolbarbutton();
        tbbtnShow.setImage("/public/images/sort_down.gif");
        tbbtnShow.setParent(comp.getParent());
        //        final Toolbarbutton tbbtnShow = (Toolbarbutton) Executions.createComponentsDirectly(
        //            "<toolbarbutton image=\"/public/images/sort_down.gif\" />", null, comp.getParent(),
        //            null);
        tbbtnShow.setVisible(false);
        tbbtnShow.addForward(Events.ON_CLICK, this.self, "onShowOrHide", null);
    }
    
    /**
     * onCheck.
     * 
     */
    public void onCheck() {
        // EvtData
        final EvtData evtData = new EvtData();
        // attr 控件根
        final Component attrComp = AttrCheckboxComp.getRootCompByCheckbox(this.self);
        // attr 顶级控件
        final AttrExt attrExt = AttrCompUtil.getAttrExtComp(attrComp);
        // 获取值,触发总控件事件
        if (((Checkbox) this.self).isChecked()) { // dischecked to checked
            this.logger.debug("fire onSubChange dischecked");
            final ArrayList<AttrInst> retList = AttrCompFactory.getInstByComponent(attrComp)
                .getValue(attrExt, attrComp);
            if (retList == null || retList.size() < 1) {
                // throw exception
                this.logger.debug("size=0");
            } else {
                evtData.setAttrExt(attrExt);
                evtData.setAttrComp(attrComp);
                evtData.setNewVal(retList.get(0));
                evtData.setAttrId(AttrCompUtil.getAttrCompAttrId(attrComp));
                evtData.setDefProdOfferId(NumUtils.toLong(""
                    + attrComp.getAttribute(AttrBaseComp.COMP_DEFPO_ID)));
                evtData.setOp(AttrCompConstants.OP_ADD);
                // 删除数据判断
                Object oldObj = AttrCheckboxComp.getRemovedObject(attrComp);
                if (oldObj != null) {
                    AttrInst oldInst = new AttrInstImpl();
                    oldInst.setData(oldObj);
                    evtData.setOldVal(oldInst);
                }
                attrExt.onSubChange(evtData);
            }
        } else { // checked to dischecked
            this.logger.debug("fire onSubChange checked");
            evtData.setAttrExt(attrExt);
            evtData.setAttrComp(attrComp);
            evtData.setAttrId(AttrCompUtil.getAttrCompAttrId(attrComp));
            evtData.setOp(AttrCompConstants.OP_REMOVE);
            // 删除数据判断
            Object oldObj = AttrCheckboxComp.getRemovedObject(attrComp);
            if (oldObj != null) {
                AttrInst oldInst = new AttrInstImpl();
                oldInst.setData(oldObj);
                evtData.setOldVal(oldInst);
            }
            attrExt.onSubChange(evtData);
        }
    }
    
    /**
     * .
     * 
     * @param event Event
     * @author wuyx
     * 2012-2-27 wuyx 
     */
    public void onShow(Event event) {
        // EvtData
        final EvtData evtData = new EvtData();
        // attr 控件根
        final Component attrComp = AttrCheckboxComp.getRootCompByCheckbox(this.self);
        Object obj = AttrCompUtil.getAttrInstData(attrComp);
        // attr 顶级控件
        final AttrExt attrExt = AttrCompUtil.getAttrExtComp(attrComp);
        // 获取值,触发总控件事件
        final ArrayList<AttrInst> retList = AttrCompFactory.getInstByComponent(attrComp).getValue(
            attrExt, attrComp);
        if (retList == null || retList.size() < 1) {
            if (obj != null) {
                evtData.setAttrExt(attrExt);
                evtData.setAttrComp(attrComp);
                AttrInst attrInst = new AttrInstImpl();
                attrInst.setData(obj);
                evtData.setNewVal(attrInst);
                evtData.setAttrId(AttrCompUtil.getAttrCompAttrId(attrComp));
                evtData.setOp(AttrCompConstants.OP_SHOW);
                attrExt.onSubChange(evtData);
            } else {
                this.logger.debug("size=0");
            }
        } else {
            evtData.setAttrExt(attrExt);
            evtData.setAttrComp(attrComp);
            evtData.setNewVal(retList.get(0));
            evtData.setAttrId(AttrCompUtil.getAttrCompAttrId(attrComp));
            evtData.setOp(AttrCompConstants.OP_SHOW);
            attrExt.onSubChange(evtData);
        }
    }
    
    /**
     * onShowOrHide.
     * 
     */
    public void onShowOrHide() {
        // EvtData
        final EvtData evtData = new EvtData();
        // attr 控件根
        final Component attrComp = AttrCheckboxComp.getRootCompByCheckbox(this.self);
        // attr 顶级控件
        final AttrExt attrExt = AttrCompUtil.getAttrExtComp(attrComp);
        // 获取值,触发总控件事件
        if (((Checkbox) this.self).isChecked()) { // dischecked to checked
            this.logger.debug("fire onSubChange onShowOrHide");
            final ArrayList<AttrInst> retList = AttrCompFactory.getInstByComponent(attrComp)
                .getValue(attrExt, attrComp);
            if (retList == null || retList.size() < 1) {
                // throw exception
                this.logger.debug("size=0");
            } else {
                evtData.setAttrExt(attrExt);
                evtData.setAttrComp(attrComp);
                evtData.setNewVal(retList.get(0));
                evtData.setAttrId(AttrCompUtil.getAttrCompAttrId(attrComp));
                evtData.setOp(AttrCompConstants.OP_DISP);
                attrExt.onSubChange(evtData);
            }
        } else { // checked to dischecked
            this.logger.debug("fire onSubChange onShowOrHide");
            evtData.setAttrExt(attrExt);
            evtData.setAttrComp(attrComp);
            evtData.setAttrId(AttrCompUtil.getAttrCompAttrId(attrComp));
            // 删除数据判断
            Object oldObj = AttrCheckboxComp.getRemovedObject(attrComp);
            if (oldObj != null) {
                AttrInst oldInst = new AttrInstImpl();
                oldInst.setData(oldObj);
                evtData.setOldVal(oldInst);
            }
            evtData.setOp(AttrCompConstants.OP_DISP);
            attrExt.onSubChange(evtData);
        }
    }
}
